﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Management/Manage.Master" %>
<%@ Import Namespace="CSI.Directory.Core.Model"%>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit Attributes
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    
    $(document).ready(function() {

    $("a").removeClass("current");
    $("#listings").addClass("current");
    var e = jQuery.Event("click");
    $("#listings").trigger(e);
    $("#list-listings").addClass("current");


    });
</script>
<h5><a href="/Management/EditListing/<%=((Listing)ViewData["Listing"]).Id %>">Back to Listing</a></h5>
    <div class="content-box">
        <!-- Start Content Box -->
        <div class="content-box-header">
            <h3>
                Edit Listing Attributes</h3>
            
            <div class="clear">
            </div>
        </div>
        <!-- End .content-box-header -->
        <div class="content-box-content">
            <div class="tab-content default-tab" id="tab1">
        
    
   <form action="/Management/EditAssociations" name="form1" method="post">
    <fieldset> <!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
								
			<p>
				<label>Categories</label>
					<%for(var i = 0; i < ((IList<ListingCategory>) ViewData["ListingCategories"]).Count; i++)
                    {%>
                    <select name="category<%=i %>">
                        <option value="">Select a Category</option>
                        <%foreach (var c in ((IList<Category>)ViewData["Categories"]).Where(t => t.ParentId == 0))
                          {%>
                                <%if (((IList<ListingCategory>)ViewData["ListingCategories"])[i].Category == c.Id)
                                  { %>
                                    <option value="<%=c.Id %>" selected="selected"><%=c.Name%></option>
                                 <%}else { %>
                                    <option value="<%=c.Id %>"><%=c.Name%></option>
                                 <%} %>
                              <%foreach (var s in ((IList<Category>)ViewData["Categories"]).Where(t => t.ParentId == c.Id))
                                {%>
                             
                             <%if (((IList<ListingCategory>)ViewData["ListingCategories"])[i].Category == s.Id)
                                  { %>
                                    <option value="<%=s.Id %>" selected="selected">---<%=s.Name%></option>
                                 <%}else { %>
                                    <option value="<%=s.Id %>">---<%=s.Name%></option>
                                 <%} %>
                         <% }%> 
                        <% }%> 
                        
                    </select>
                    
	            <br /><br />
	            <%} %>
	            <%for(var i = ((IList<ListingCategory>) ViewData["ListingCategories"]).Count; i < 10; i++){%>
	            
	                <select name="category<%=i %>">
                        <option value="">Select a Category</option>
                        <%foreach (var c in ((IList<Category>)ViewData["Categories"]).Where(t => t.ParentId == 0))
                          {%>
                                
                                    <option value="<%=c.Id %>"><%=c.Name%></option>
                                 
                              <%foreach (var s in ((IList<Category>)ViewData["Categories"]).Where(t => t.ParentId == c.Id))
                                {%>
                             
                             
                                    <option value="<%=s.Id %>">---<%=s.Name%></option>
                                 <%} %>
                         <% }%> 
                        
                        
                    </select>
                    
	            <br /><br />
	            <%} %>
			</p>
			<p>
			<label>Counties</label>
					<%for (var i = 0; i < ((IList<ListingCounty>)ViewData["ListingCounties"]).Count; i++)
                    {%>
                    <select name="county<%=i %>">
                        <option value="">Select a County</option>
                        <%foreach (var c in ((IList<County>)ViewData["Counties"]))
                          {%>
                                <%if (((IList<ListingCounty>)ViewData["ListingCounties"])[i].County == c.Id)
                                  { %>
                                    <option value="<%=c.Id %>" selected="selected"><%=c.Name%></option>
                                 <%}else { %>
                                    <option value="<%=c.Id %>"><%=c.Name%></option>
                                 <%} %>
                             
                            
                         <% }%> 
                        
                        
                    </select>
                    
	            <br /><br />
	            <% }%> 
			</p>
            
            <input name="id" value="<%=((Listing)ViewData["Listing"]).Id %>" type="hidden" />
             <input type="button" onclick="return DoAjaxSubmit(this);" value="Save" class="button" />
             <div id="results"></div>
            </fieldset>
   </form>
    </div>
        </div>
    </div>
    
    <script type="text/javascript">
    
        function DoAjaxSubmit(btnClicked) {
            var form = $(btnClicked).parents('form');
            if ($("#name").val() == "" || $("#body").val() == "") {
                $("#results").html("Please complete all fields").addClass("red");
                return false;
            }
            else {
                $("#results").html("");
                $.ajax({
                    type: "POST",
                    url: form.attr('action'), //'<%=ResolveUrl("~/register/go") %>',
                    data: form.serialize(),
                    error: function(xhr, status, error) {
                    },
                    success: function(response) {
                        if (response != null) {
                            if (!response.Successful) {
                                $("#results").html("An error occurred: " + response.Exception).addClass("red");
                                return false;
                            }
                            else {
                                $("#results").html("Listing updated").addClass("green");
                                
                            }
                        }
                    }
                });
            }
            return false;
        }
    </script>

</asp:Content>
